package spring.angularjs.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.angularjs.domain.Addressbook;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value = "/address")
public class AddressBookController {
	
	@Autowired
	private Services services;
	
	@RequestMapping(value="/start")
	public String getAddressBookPartialPage(){
		return "addressbook/layout";
	}
	
	@RequestMapping(value = "/add", method=RequestMethod.POST, headers="Accept=application/json")
	@ResponseBody
	public void addAddressBook(@RequestBody Addressbook ab){
		services.addAddressBook(ab);
	}

	@RequestMapping(value = "/all.json", method = RequestMethod.GET, headers="Accept=application/json")
	@ResponseBody
	public List<Addressbook> getListAddressBook(){
		return services.getListAddressbook();
	}
	
	@RequestMapping(value = "/get/{id}", method =RequestMethod.GET, headers="Accept=application/json")
	@ResponseBody
	public Addressbook getAddressBook(@PathVariable("id") int id){
		return services.getAddressbook(id);
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.PUT, headers="Accept=application/json")
	@ResponseBody
	public void updateAddressBook(@RequestBody Addressbook ab){
		services.updateAddressbook(ab);
	}
	
	@RequestMapping(value = "/delete/{id}", method=RequestMethod.DELETE, headers="Accept=application/json")
	@ResponseBody
	public void deleteAddressBook(@PathVariable("id") int id){
		Addressbook ab = services.getAddressbook(id);
		services.deleteAddressBook(ab);
	}
	
	@RequestMapping(value = "/deleteAll", method=RequestMethod.DELETE, headers="Accept=application/json")
	@ResponseBody
	public void deleteAllAddressBook(){
		services.deleteAllAddressBook();
	}
}
